package com.example.martian.controller;

import com.example.martian.config.util.RedisUtils;
import com.example.martian.service.RegisterService;
import com.example.martian.utils.GuGeUtils;
import com.example.martian.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/goole/")
@CrossOrigin
public class GooleController {

    @Autowired
    private GuGeUtils guGeUtils;

    @Autowired
    private RegisterService registerService;

    private static String secret = "SWCDDQXDGIQPHOSA";

    //发送验证
    @ResponseBody
    @RequestMapping(value = "sendGoolenum")
    public Object sendGooleNum(String address) {
        secret = guGeUtils.generateSecretKey();
        // 把这个qrcode生成二维码，用google身份验证器扫描二维码就能添加成功
        String qrcode = guGeUtils.getQRBarcode(address, secret);
        System.out.println("qrcode:" + qrcode + ",key:" + secret);
        return Utils.toJSON("发送成功", qrcode, "0");
    }

    //验证谷歌
    @ResponseBody
    @RequestMapping(value = "goolenum")
    public Object gooleNum(String address,Long code,String addressCode) {
        long time = System.currentTimeMillis();
        GuGeUtils goole = new GuGeUtils();
        goole.setWindowSize(5);
        boolean bool = goole.check_code(secret, code, time);
        Boolean aBoolean = registerService.verificationMessage(addressCode, address);
        System.out.println("检查code是否正确？" + bool);
        if (!bool){
            if (!aBoolean){
                return Utils.toJSON("请输入正确的验证码",Utils.toJSONmap(),"2");
            }
            return Utils.toJSON("谷歌验证码错误",Utils.toJSONmap(),"1");
        }
        return Utils.toJSON("验证成功",Utils.toJSONmap(),"0");
    }
}


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      